<html>
  <head>
    <title>交易系统－－订单查询</title>
  </head>
  <body>
    <div id="parameterTabs" class="x-form-item">
    </div>
    <div id="queryOrderGrid" class="x-form-item">
    </div>
    <div id="queryTicketGrid" class="x-form-item">
    </div>
    <script>
        var webOrderDS;
        Ext.onReady(function(){
            // second tabs built from JS
            tabs.getItem(0).setTitle('订单综合查询');;
            var query_order_tabs = new Ext.TabPanel({
                id: 'query_order_tabs',
                renderTo: 'parameterTabs',
                activeTab: 0,
                width: '100%',
                height: 60,
                plain: true,
                defaults: {
                    autoScroll: true
                },
                items: [{
                        id: 'query_order_tab1',
                        title: '订单号查询',
                        autoLoad: {
                            url: '/web_admin/search_by_orderid',
                            scripts: true
                        }
                    }, {
                        id: 'query_order_tab2',
                        title: 'PNR查询',
                        autoLoad: {
                            url: '/web_admin/search_by_pnr',
                            scripts: true
                        }
                    }, {
                        id: 'query_order_tab3',
                        title: '乘机人信息查询',
                        autoLoad: {
                            url: '/web_admin/search_by_pass',
                            scripts: true
                        }
                    }, {
                        id: 'query_order_tab4',
                        title: '复杂查询',
                        autoLoad: {
                            url: '/web_admin/search_by_other',
                            scripts: true
                        }
                    }]
            });


            //############## 订单Grid ##############
            var orderCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                    header: '编号',
                    dataIndex: 'id'
                }, {
                    header: '乘机人',
                    dataIndex: 'pass',
                    width: 120
                }, {
                    header: '定单号',
                    dataIndex: 'order_no',
                    width: 100
                }, {
                    header: 'PNR',
                    dataIndex: 'pnr',
                    width: 50
                }, {
                    header: '航班号',
                    dataIndex: 'air_no',
                    width: 60
                }, {
                    header: '始发地',
                    dataIndex: 'from',
                    width: 90
                }, {
                    header: '目的地',
                    dataIndex: 'to',
                    width: 90
                }, {
                    header: '乘机日期',
                    dataIndex: 'fly_date',
                    width: 70
                }, {
                    header: '起飞(T)',
                    dataIndex: 'fly_time',
                    width: 50
                }, {
                    header: '预定时间',
                    dataIndex: 'created_at',
                    width: 120
                }, {
                    header: '平台款',
                    dataIndex: 'platform_amount',
                    width: 40
                }, {
                    header: '支付状态',
                    dataIndex: 'trx_status',
                    width: 70
                }, {
                    header: '订单状态',
                    dataIndex: 'status',
                    width: 70
                },{
                    header: '锁定状态',
                    width: 70,
                    dataIndex: 'fromto',
                    renderer: isLockedRender
                },{
                    header: '操作',
                    width: 50,
                    dataIndex: 'fromto',
                    renderer: operationRender
                },{
                    header: '锁定',
                    width: 50,
                    dataIndex: 'fromto',
                    renderer: operationlockRender
                }]);
            function operationRender(value, cellmeta, record, rowIndex, columnIndex, store){
                var content = "";
                if (record.get("trx_status")=="等待支付"){
                    content += "<a href=\"/web_admin/agentReadyPay?web_order_no="+record.get("order_no")+"&fromto="+record.get("fromto")+"\" target=_blank>马上支付</a>||";
                }
                content += "&nbsp;<a href=# onclick='delWebOrder(\""+record.get("id")+"\")'>取消</a>";
                return content
            }
            function isLockedRender(value, cellmeta, record, rowIndex, columnIndex, store){
                if (record.get("is_locked")=="0"){
                    return "非锁定";
                }else{
                    return "<span class='red'>锁定</span>";
                }
            }
            function operationlockRender(value, cellmeta, record, rowIndex, columnIndex, store){
                if (record.get("is_locked")=="0"){
                    return "<a href=# onclick='setWebOrderLock(\""+record.get("order_no")+"\",true)'>锁定</a>";
                }else{
                    return "<a href=# onclick='setWebOrderLock(\""+record.get("order_no")+"\",false)'>解锁</a>";
                }
            }

            delWebOrder = function(id){
                Ext.Ajax.request({
                    url: '/web_admin/cancel_order',
                    params: 'id='+id,
                    method: 'GET',
                    success: function(result, request){
                        if (result.responseText.indexOf("成功") >= 0) {
                            Ext.MessageBox.alert('操作成功', result.responseText);
                            webOrderDS.reload();
                            ticketds.baseParams.id = "";
                            ticketds.reload();
                        }
                        else {
                            Ext.MessageBox.alert('操作失败', result.responseText);
                        }
                    },
                    failure: function(result, request){
                        Ext.MessageBox.alert('操作失败', '取消订单失败');
                    }
                });
            }
            //隐藏order数据库id
            orderCM.setHidden(1, !orderCM.isHidden(1));
            webOrderDS = new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({
                    url: '/web_admin/getAllWebOrdersBySearch'
                }),
                reader: new Ext.data.JsonReader({
                    totalProperty: 'totalProperty',
                    root: 'root'
                }, [{
                        name: 'id'
                    }, {
                        name: 'pass'
                    }, {
                        name: 'order_no'
                    }, {
                        name: 'pnr'
                    }, {
                        name: 'trx_status'
                    }, {
                        name: 'status'
                    }, {
                        name: 'air_no'
                    }, {
                        name: 'from'
                    }, {
                        name: 'to'
                    }, {
                        name: 'fly_date'
                    }, {
                        name: 'fly_time'
                    }, {
                        name: 'created_at'
                    }, {
                        name: 'fromto'
                    }, {
                        name: 'platform_amount'
                    },{
                        name: 'is_locked'
                    }])
            });

            var ordergridpanel = new Ext.grid.GridPanel({
                el: 'queryOrderGrid',
                ds: webOrderDS,
                cm: orderCM,
                //sm: sm,
                loadMask: true,
                autoWidth: true,
                collapsible: true,
                animCollapse: true,
                height: 300,
                title: '订单信息',
                bbar: new Ext.PagingToolbar({
                    pageSize: 10,
                    store: webOrderDS,
                    displayInfo: true,
                    displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                    emptyMsg: "没有记录"
                })

            });

            ordergridpanel.render();
            //ds.load();
            webOrderDS.load({
                params: {
                    start: 0,
                    limit: 10
                }
            });
            var ordersrow;
            var current_order_id;
            //双击订单查看机票详细
            ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
                var current_order_selected = webOrderDS.data.items[rowIndex].id;
                ordersrow = webOrderDS.getById(current_order_selected);//ds为绑定到grid的数据源
                current_order_id = ordersrow.get("id");
                ticketds.baseParams.id = current_order_id;
                ticketds.reload();
            });

            //### 机票Grid ###
            var ticketcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                    header: '编号',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: 'PNR',
                    dataIndex: 'pnr',
                    width: 60
                }, {
                    header: '旅客姓名',
                    dataIndex: 'pass_name',
                    width: 70
                }, {
                    header: '证件号码',
                    dataIndex: 'pass_card_id',
                    width: 140
                }, {
                    header: '票号',
                    dataIndex: 'ticket_no',
                    width: 110
                    /*
                    }, {
                        header: '状态',
                        dataIndex: 'status',
                        width: 50
                     */
                }, {
                    header: '机票价格',
                    dataIndex: 'price',
                    width: 60
                }, {
                    header: '机场费税',
                    dataIndex: 'airdrome_tax',
                    width: 60
                }, {
                    header: '附加费',
                    dataIndex: 'fuel_tax',
                    width: 60
                    /*
                    }, {
                        header: '保险份数',
                        dataIndex: 'insurance_number',
                        width: 60
                     */
                }, {
                    header: '其他',
                    dataIndex: 'other',
                    width: 50
                }, {
                    header: '合计',
                    dataIndex: 'total_price',
                    width: 60
                },{
                    header: '修改证件',
                    width: 70,
                    renderer: modCardRender
                },{
                    header: '取消',
                    width: 60,
                    renderer: delRender
                }]);
            ticketcm.defaultSortable = true;
            var ticketds = new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({
                    url: '/web_admin/getTicketsByOrderID'
                }),
                reader: new Ext.data.JsonReader({
                    //totalProperty: 'totalProperty',
                    root: 'root'
                }, [{
                        name: 'id'
                    }, {
                        name: 'pnr'
                    }, {
                        name: 'web_order_id'
                    }, {
                        name: 'pass_name'
                    }, {
                        name: 'pass_tel'
                    }, {
                        name: 'pass_card_id'
                    }, {
                        name: 'ticket_no'
                    }, {
                        name: 'price'
                    }, {
                        name: 'status'
                    }, {
                        name: 'airdrome_tax'
                    }, {
                        name: 'fuel_tax'
                    }, {
                        name: 'other'
                    }, {
                        name: 'total_price'
                    }, {
                        name: 'insurance_campany'
                    }, {
                        name: 'insurance_out_date'
                    }, {
                        name: 'insurance_number'
                    }, {
                        name: 'insurance_no'
                    }, {
                        name: 'insurance_type'
                    }, {
                        name: 'is_locked'
                    }])
            });

            var ticketgridpanel = new Ext.grid.GridPanel({
                el: 'queryTicketGrid',
                ds: ticketds,
                cm: ticketcm,
                loadMask: true,
                autoWidth: true,
                collapsible: true,
                animCollapse: true,
                height: 180,
                title: '乘客航班信息'
            });

            ticketgridpanel.render();
            ticketds.load();

            function modCardRender(value, cellmeta, record, rowIndex, columnIndex, store){
                if (record.get("status")=="处理完成"){
                    return ""
                }else{
                    var cur_ticket_id = record.get("id");
                    return "<a href=# onclick='modWebPassCard(\""+cur_ticket_id+"\")'>修改证件号</a>";
                }
            }
            function delRender(value, cellmeta, record, rowIndex, columnIndex, store){
                if (record.get("status")=="处理完成"){
                    return "";
                }else{
                    var cur_ticket_id = record.get("id");
                    return "<a href=# onclick='delTicket(\""+cur_ticket_id+"\")'>取消</a>";
                }
            }

            delTicket = function(id){
                Ext.Ajax.request({
                    url: '/web_admin/cancel_ticket',
                    params: 'id='+id,
                    method: 'GET',
                    success: function(result, request){
                        if (result.responseText.indexOf("成功") >= 0) {
                            Ext.MessageBox.alert('操作成功', '取消乘客成功');
                            webOrderDS.reload();
                            ticketds.baseParams.id = "";
                            ticketds.reload();
                        }
                        else {
                            Ext.MessageBox.alert('操作失败', result.responseText);
                        }
                    },
                    failure: function(result, request){
                        Ext.MessageBox.alert('操作失败', '取消乘客失败');
                    }
                });
            }
            modWebPassCard = function(id){
                var id_card = new Ext.form.TextField({
                    fieldLabel: '证件号码',
                    name: 'pass_card_id',
                    allowBlank: true,
                    anchor: '90%'
                });

                var formPanel = new Ext.form.FormPanel({
                    //baseCls: 'x-plain',
                    labelWidth: 60,
                    labelAlign: 'right',
                    frame: true,
                    url: "/web_admin/saveEditTicket?id=" + id,
                    reader: new Ext.data.JsonReader({
                        root: 'root'
                    }, ['pass_card_id']),
                    items: [id_card]
                });

                formPanel.form.load({
                    url: '/web_admin/getEditTicketInfoByID?id=' + id,
                    waitMsg: '正在载入数据...',
                    success: function(form, action){
                        //Ext.MessageBox.alert('提示信息', '信息加载成功！');
                    },
                    failure: function(form, action){
                        Ext.MessageBox.alert('提示信息', '信息加载失败');
                    }
                });

                var fixWindow = new Ext.Window({
                    title: '编辑证件号码',
                    width: 260,
                    height: 130,
                    layout: 'fit',
                    plain: true,
                    bodyStyle: 'padding:5px;',
                    buttonAlign: 'center',
                    items: formPanel,
                    buttons: [{
                            text: '保存',
                            handler: function(){
                                // check form value
                                if (formPanel.form.isValid()) {
                                    formPanel.form.submit({
                                        waitMsg: 'In processing',
                                        success: function(form, action){
                                            var info = "操作失败，具体失败原因请查询系统日志";
                                            if (action.result && action.result.info && action.result.info != "") {
                                                info = action.result.info;
                                            }
                                            if (info.indexOf("成功") >= 0) {
                                                Ext.MessageBox.alert('编辑成功', info);
                                                fixWindow.hide();
                                                ticketds.reload();
                                            }else{
                                                Ext.MessageBox.alert('编辑失败', info);
                                            }
                                        },
                                        failure: function(form, action){
                                            Ext.MessageBox.alert('操作信息', '操作失败');
                                        }
                                    });
                                }
                                else {
                                    Ext.MessageBox.alert('错误消息', '请将必填项信息录入后再提交.');
                                }
                            }
                        }, {
                            text: '取消',
                            handler: function(){
                                fixWindow.hide();
                            }
                        }]
                });
                fixWindow.show();
            }
        });
        function setWebOrderLock(webOrderNo,isLocked){
            Ext.Ajax.request({
                url: '/web_admin/setWebOrderLock?order_no='+webOrderNo+"&islocked="+isLocked,
                method: 'GET',
                success: function(result, request){
                    if (result.responseText == "SUCCESS") {
                        webOrderDS.load({
                            params: {
                                start: 0,
                                limit: 10
                            }
                        });
                    }
                },
                failure: function(result, request){
                    Ext.MessageBox.alert('消息', '通讯失败,请稍后再试,或联系管理员!');
                }
            });
        }
    </script>
  </body>
</html>
